// api/user_api/login.go
package user_api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
	"muziai-server/utils/res"
)

var store = base64Captcha.DefaultMemStore

type CaptchaResponse struct {
	Image   string `json:"image"`
	ImageId string `json:"imageId"`
}

func (u UserApi) CaptchaView(c *gin.Context) {
	// 图片验证码
	driver := &base64Captcha.DriverString{
		Height:          80,
		Width:           240,
		NoiseCount:      5,
		ShowLineOptions: 5,
		Length:          4,
		Source:          "0123456789",
		BgColor:         nil,
		Fonts:           nil,
	}
	capt := base64Captcha.NewCaptcha(driver, store)
	id, b64s, answer, err := capt.Generate()
	fmt.Println(answer)
	if err != nil {
		res.FailWithMsg(c, err.Error())
		return
	}
	res.OkWithData(c, CaptchaResponse{
		Image:   b64s,
		ImageId: id,
	})
}
